﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using FinPlusAssembler;
using FinPlusInterfaces;
using FinPlusCompCore;

namespace FinPlusCompProxy
{
    public class QLEngineZCM : FinPlusComponent
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }

        private enum Params { Instrument }
        private readonly IFinPlusComp _instrumentBuildConn, _instrumentConn;
        private readonly QLAnalytics _analytics;
        private readonly Instruments _instruments;
        private readonly BuildEvent _buildEvents;

        //construct
        public QLEngineZCM(IFinPlusComp analytics, IFinPlusComp instrumentBuilds, IFinPlusComp instruments, BuildEvent buildEvents)
        {
            _analytics = (QLAnalytics)analytics;
            _instrumentBuildConn = instrumentBuilds;
            _instrumentConn = instruments;
            _buildEvents = buildEvents;
            _instruments = new Instruments();

            foreach (var instrument in _instrumentConn.Adapter.Where("Name='InstrumentType' And Value='ZCM'", Params.Instrument.ToString()))
                _instruments[instrument.Key] = new Instrument(instrument.Key, instrument.Value, _instrumentBuildConn.Adapter.First("Name='Instrument' And Value='" + instrument.Key + "'"), _analytics, _buildEvents);

            IsValid = true;
        }

        //common control interface
        public override void Dispose()
        {
            try
            {
                if (_analytics != null)
                    _analytics.Dispose();
                if (_instrumentConn != null)
                    _instrumentConn.Dispose();
                if (_instrumentBuildConn != null)
                    _instrumentBuildConn.Dispose();
            }
            catch (Exception e)
            {
                IsValid = Level.Warning.Log(Id, Config, e.Message, e);
            }
        }
    }
}
